const app = getApp();
const api = require("../../utils/api");

Page({
    data: {
        type: 0, //0 切换店铺 1 选择提货店铺

        shopId: '',
        departmentId: '',
        storeList: [],
        storeSelected: '', //已选店铺
        keywords: '',
        isShowFrequentlyUsed: false,

        pageNum: 1,
        pageSize: 8,
        total: 0,
        noContent: false, //无数据
        isFinished: false, //加载结束
    },
    onLoad(options) {
        //保存店铺id
        this.setData({
            type: options.dir ? options.dir : 0, //0 切换店铺 1 选择提货店铺
            shopId: options.shopId ? parseInt(options.shopId) : '',
            departmentId: options.departmentId ? options.departmentId : '',
        });

        if(this.data.type) {
            wx.setNavigationBarTitle({
                title: '选择提货店铺'
            });
        } else if(this.data.departmentId) {
            wx.setNavigationBarTitle({
                title: '切换店铺'
            });
        } else if(this.data.shopId) {
            this.getStoreInfo();

            wx.setNavigationBarTitle({
                title: '切换社区'
            });
        } else {
            wx.setNavigationBarTitle({
                title: '选择社区'
            });
        }
        this.getStoreList(); //获取店铺列表
    },
    onShow() {
        
    },

    getStoreList() { //获取店铺列表
        let params = {
            departmentId: this.data.departmentId,
            shopName: this.data.keywords,
            shopType: this.data.departmentId ? 0 : 1, //店铺类型：0普通店铺 1社区自营 2平台自营
            shopPickupStatus: this.data.type ? 1 : '',
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        }
        api.post('/index/shop/recommend', params)
            .then(res => {
                let list = res.content;
                list.forEach(item => {
                    if(item.shopId == this.data.shopId) {
                        item.selected = true;
                        this.setData({
                            storeSelected: item,
                        });
                    } else {
                        item.selected = false;
                    }
                    //整理营业时间
                    let businessBeginTime = item.businessBeginTime.split(':');
                    let businessEndTime = item.businessEndTime.split(':');
                    item.businessTime = businessBeginTime[0] + ':' + businessBeginTime[1] + '~' + businessEndTime[0] + ':' + businessEndTime[1];
                });

                let newList = [...this.data.storeList, ...list];
                this.setData({
                    storeList: newList,
                    total: res.total
                });

                //是否无内容标识
                if(this.data.storeList.length == 0) {
                    this.setData({
                        noContent: true
                    })
                } else {
                    this.setData({
                        noContent: false
                    });
                }
                //是否加载完毕标识
                if(this.data.storeList.length >= this.data.total) {
                    this.setData({
                        isFinished: true
                    })
                } else {
                    this.setData({
                        isFinished: false
                    })
                }
                wx.stopPullDownRefresh();
            })
    },
    getStoreInfo() { //获取店铺信息
        api.post('/shop/detail?shopId=' + this.data.shopId)
            .then(res => {
                this.setData({
                    storeSelected: res,
                });
            })
    },
    chooseStore(e) { //选择店铺
        let index = e.currentTarget.dataset.index;
        let list = this.data.storeList;
        list.forEach((item, i) => {
            if (i == index) {
                item.selected = true;
                this.setData({
                    storeSelected: item
                });
            } else {
                item.selected = false;
            }
        });
        this.setData({
            storeList: list
        });

        if(this.data.type == 1) { //选择提货店铺
            wx.setStorageSync('pickup', this.data.storeList[index].shopId); //保存店铺id
            app.globalData.isRefreshPickipStore = true; //刷新店铺标识

            wx.showToast({
                title: '店铺选择成功！',
                icon: 'success'
            });
            setTimeout(() => {
                wx.navigateBack(-1);
            }, 1000);
        } else { //切换店铺
            wx.setStorageSync('shopId', this.data.storeList[index].shopId); //保存店铺id
            wx.showToast({
                title: this.data.departmentId ? '店铺切换成功！' : '社区切换成功！',
                icon: 'success'
            });
            if(this.data.departmentId) {
                app.globalData.isRefreshCategory = true;
            } else {
                app.globalData.isRefreshStore = true;
            }
            setTimeout(() => {
                wx.navigateBack(-1);
            }, 1000);
        }
    },

    getFrequentlyUsedStoreList() { //获取常用店铺列表
        api.post(`/user/pickup/address/list`)
            .then(res => {
                let list = res;
                list.forEach(item => {
                    if(item.shopId == this.data.shopId) {
                        item.selected = true;
                        this.setData({
                            storeSelected: item,
                        });
                    } else {
                        item.selected = false;
                    }
                });
                this.setData({
                    storeList: list,
                    isFinished: true,
                    noContent: list.length == 0 ? true : false
                });
            })
    },

    bindInput(e) {
        let val = e.detail.value;
        this.setData({
            keywords: val
        });
    },
    submitSearch() { //搜索店铺
        this.setData({
            storeList: [],
            pageNum: 1,
            total: 0,
            isFinished: false,
            isShowFrequentlyUsed: false,
        });
        this.getStoreList();
    },
    switchFrequentlyUsedStore() { //查看(或关闭)常用店铺
        this.setData({
            isShowFrequentlyUsed: !this.data.isShowFrequentlyUsed,
            storeList: [],
            pageNum: 1,
            total: 0,
            isFinished: false,
        });

        if(this.data.isShowFrequentlyUsed) {
            this.getFrequentlyUsedStoreList();
        } else {
            this.getStoreList();
        }
    },

    clearChoose() { //清除已选店铺
        let list = this.data.storeList;
        list.forEach(item => {
            if (item.id == this.data.storeSelected.id) {
                item.selected = false;
            }
        });
        this.setData({
            storeList: list,
            storeSelected: ''
        });

        wx.removeStorageSync('shopId'); //清除店铺id
        app.globalData.isRefreshStore = true; //刷新店铺标识
        app.globalData.isRefreshCategory = true; //刷新分类标识
    },

    onPullDownRefresh() {
        //下拉刷新
        this.setData({
            storeList: [],
            pageNum: 1,
            total: 0,
            isFinished: false,
        });
        this.getStoreList();
    },
    onReachBottom() {
        //加载更多
        if(!this.data.isFinished) {
            this.setData({
                pageNum: this.data.pageNum+1,
            });
            
            setTimeout(() => {
                this.getStoreList();
            }, 800);
        }
    },
})